import { t } from 'i18next'
import { useTvWallStore } from '../store'
import shallow from 'zustand/shallow'
import { Modal } from '@kedacom/react-next'
import { revert_mt } from '@/api/manager/mt'
import { revert_poll } from '@/api/manager/poll/interface'
import { SinglePollData } from '@/components/SinglePollModal'
import { confirmModal } from '@/components/Alert'
export const useBoxViewHook = () => {
	const { boxDetailVOsSource, operateBoxInfo, show_mt_name, changeTVWallChannel, removeStreamDom } = useTvWallStore(
		(state) => ({
			update: state.update,
			my_used_style: state.my_used_style,
			correct_style: state.correct_style,
			config: state.config,
			style_map: state.style_map,
			boxDetailVOsSource: state.boxDetailVOsSource,
			operateBoxInfoIndex: state.operateBoxInfoIndex,
			operateBoxInfo: state.operateBoxInfo,
			show_mt_name: state.show_mt_name,
			changeTVWallChannel: state.changeTVWallChannel,
			removeStreamDom: state.removeStreamDom,
		}),
		shallow
	)
	/** 轮询终端已选择个数 */
	const getPollMembersNum = () => {
		return operateBoxInfo?.poll.members.length || 0
	}
	/**得到本通道之外的轮询终端*/
	const getMtPollmap = () => {
		let pollMap: { [key: string]: any } = {}
		let mccTVWallPolls = boxDetailVOsSource.filter(
			(item) => item.member_type === 5 && item.hdu_id !== operateBoxInfo?.hdu_id
		)
		for (let i = 0; i < mccTVWallPolls.length; i++) {
			let current = mccTVWallPolls[i]
			let poll = current.poll
			if (poll) {
				let pollMts = poll.members || [] // 轮询成员列表
				// 以终端的mt_key作为件键  终端信息作为value值
				for (let k = 0; k < pollMts.length; k++) {
					let key = pollMts[k].mt_key + ''
					pollMap[key] ? pollMap[key].push(poll) : (pollMap[key] = [poll])
				}
			}
		}
		return pollMap
	}
	const handleRequestChangeSinglePoll = (_poll: SinglePollData) => {
		/**改变单通道轮询 */
		if (!operateBoxInfo) return
		/**证明单通道轮询数据存在*/
		const _members = boxDetailVOsSource
			.map((item) => {
				if (item.hdu_id === operateBoxInfo.hdu_id) {
					return {
						member_type: 5,
						hdu_id: operateBoxInfo.hdu_id,
						poll: _poll.poll ? revert_poll(_poll.poll) : null,
					}
				}
				return null
			})
			.filter(Boolean)
		/**更改单通道轮询 */
		changeTVWallChannel({
			mode: 1,
			show_mt_name: show_mt_name,
			batch_poll: null,
			members: _members,
		})
		/**移除多流 */
		removeStreamDom()
	}
	/**判断选择终端是否在其他通道当中已设置*/
	const handleConfirmSingle = (_poll: SinglePollData) => {
		let mtPollMap = getMtPollmap() /**其他盒子设置的单通道轮询终端 */
		let mts = _poll.poll?.members || [] /**当前单通道轮询的数据 */
		let _mts = mts.map(revert_mt)
		let confictMt: Array<string> = []
		_mts.forEach((item) => {
			let _key = item.mt_key
			if (mtPollMap[_key] && mtPollMap[_key].length > 0) {
				confictMt.push(item.name || item.account) /**添加进入冲突终端 */
			}
		})
		if (confictMt.length > 0) {
			let _name = ''
			if (confictMt.length <= 4) {
				_name = confictMt.join(',')
			} else {
				_name = confictMt.slice(0, 4).join(',') + '...'
			}
			confirmModal({
				msg: t('{{name}}在其他通道中已设置轮询，是否重复设置？', { name: _name }),
				onOk: () => {
					handleRequestChangeSinglePoll(_poll)
				},
			})
		} else {
			handleRequestChangeSinglePoll(_poll)
		}
	}
	return {
		/** 格子轮询终端已选择个数 */
		getPollMembersNum,
		/**检测单通道轮询冲突 */
		handleConfirmSingle,
	}
}
